package com.intelligent.health.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.health.member.dto.UserDto;
import com.intelligent.health.member.service.MemberService;
import com.intelligent.health.member.vo.UserVo;
import entity.R;
import entity.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: torlesse-liang
 * @Date: 2022/03/17/22:40
 * @Description: 用户信息管理
 */
@Slf4j
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 获取用户信息 以及角色权限信息
     *
     * @param username 用户名
     * @return 用户账号信息以及权限信息
     */
    @ApiOperation("用户账号信息以及权限信息")
    @ApiImplicitParam(name = "username",value = "torlesse",required = true,dataType = "String",paramType = "query")
    @GetMapping("/authInfo")
    public R getUserAndAuthInfo(@RequestParam("username") String username) {
        HashMap<String, Object> hashMap = memberService.getUserAuthInfo(username);
        return R.ok().data(hashMap);
    }

    /**
     * 分页查询符合查询条件的用户信息集合
     *
     * @param userDto 查询条件
     * @param pageNum 页码
     * @param pageSize 页面大小
     * @return 用户信息集合
     */
    @PostMapping("/all/{pageNum}/{pageSize}")
    public R queryMembersByPage(@RequestBody UserDto userDto,
                                @PathVariable("pageNum") Integer pageNum,
                                @PathVariable("pageSize") Integer pageSize) {
        IPage<UserVo> users = memberService.queryMembersByPage(userDto, pageNum, pageSize);
        return R.ok().data("users",users);
    }

    /**
     * 根据用户id查询用户
     *
     * @param userId 用户id
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    public R queryMemberById(@PathVariable("userId") Long userId) {
        UserVo userVo = memberService.queryMemberById(userId);
        return R.ok().data("userVo",userVo);
    }

    /**
     * 更新/编辑用户个人信息
     *
     * @param userDto 用户更新的信息
     */
    @PutMapping("/edit")
    public R editMemberInfo(@RequestBody UserDto userDto) {
        try {
            memberService.editMemberInfo(userDto);
        } catch (Exception e) {
            log.error("更新个人信息失败: {}", userDto);
            return R.setResult(ResultCodeEnum.UPDATE_ERROR);
        }
        log.error("更新个人信息成功: {}", userDto);
        return R.setResult(ResultCodeEnum.UPDATE_SUCCESS);
    }

    /**
     * 逻辑删除用户 设置station为无效 0
     *
     * @param userId 用户id
     */
    @DeleteMapping("/del/{userId}")
    public R delMember(@PathVariable("userId") Long userId) {
        try {
            memberService.delMember(userId);
        } catch (Exception e) {
            log.error("删除用户失败: {}", userId);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.error("删除用户成功: {}", userId);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }

    /**
     * 批量逻辑删除用户
     *
     * @param userIds 用户组id
     */
    @DeleteMapping("/delMore")
    public R delMoreMember(ArrayList<Long> userIds) {
        try {
            memberService.delBatchMember(userIds);
        } catch (Exception e) {
            log.error("批量删除用户失败: {}", userIds);
            return R.setResult(ResultCodeEnum.DELETE_ERROR);
        }
        log.info("批量删除用户成功: {}", userIds);
        return R.setResult(ResultCodeEnum.DELETE_SUCCESS);
    }
}
